/*
 * @Author: superRice
 * @Date: 2025-03-11 18:02:07
 * @LastEditTime: 2025-05-27 19:27:07
 * @FilePath: \threejs-testing-dedicated\examples\changeVertexMaterial\index.js
 * @Description:
 * -----愿你走过所有风雨，归来仍少年----
 * Copyright (c) 2025 by 造梦星空, All Rights Reserved.
 */
import * as TE from 'TEngine';
import * as THREE from 'three';
import { Pane } from 'tweakpane';
import CustomShaderMaterial from 'three-custom-shader-material/vanilla';
import indexFrag from './shader/index.frag';
import indexVert from './shader/index.vert';

const TS = new TE.TEngine();

TS.camera.position.set(10, 10, 10);
TS.camera.lookAt(0, 0, 0);
TS.camera.updateProjectionMatrix();

// const axes = new THREE.AxesHelper(5);
// TS.scene.add(axes);

//创建一个全局光
const light = new THREE.AmbientLight(0xffffff, 3);

TS.scene.add(light);

//创建一个方块

const geometry = new THREE.BoxGeometry(10, 10, 10);
const geometry2 = new THREE.BoxGeometry(10, 10, 10);

//在这个方块的每个顶点上添加一个颜色

//写一个颜色
const colorValue = 'yellow';

const colors = [];
for (let i = 0; i < geometry.attributes.position.count; i++) {
  //生成随机颜色
  const color = new THREE.Color(colorValue);
  colors.push(color.r, color.g, color.b);
}
geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
geometry2.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));

//写一个新的属性叫使用条纹
geometry.setAttribute(
  'useStripes',
  new THREE.Float32BufferAttribute(new Array(geometry.attributes.position.count).fill(1), 1),
);

const material = new THREE.MeshStandardMaterial({
  metalness: 0.25,
  roughness: 1.0,
  color: 0xffffff,
  transparent: true,
  opacity: 1,
  vertexColors: true,
});

const customMaterial = new CustomShaderMaterial({
  baseMaterial: material,
  vertexShader: indexVert,
  fragmentShader: indexFrag,
});

const cube = new THREE.Mesh(geometry, customMaterial);
const cube2 = new THREE.Mesh(geometry2, customMaterial);
TS.scene.add(cube);
TS.scene.add(cube2);
cube2.position.set(15, 0, 0);
